package com.uliky.shouyiLibrary.product.service.impl;

import com.uliky.shouyiLibrary.product.entity.AttrEntity;
import com.uliky.shouyiLibrary.product.service.AttrService;
import com.uliky.shouyiLibrary.product.vo.AttrtGroupWithAttrsVo;
import com.uliky.shouyiLibrary.product.vo.SkuItemVo;
import com.uliky.shouyiLibrary.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uliky.common.utils.PageUtils;
import com.uliky.common.utils.Query;

import com.uliky.shouyiLibrary.product.dao.AttrGroupDao;
import com.uliky.shouyiLibrary.product.entity.AttrGroupEntity;
import com.uliky.shouyiLibrary.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据三级分类id查询商品
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)) {
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key) ;
            });
        }
        if(catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return  new PageUtils(page);
        }

    }

    /**
     * 根据分类id查出分组以及所有属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrtGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1.查出当前分类的所有属性分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrtGroupWithAttrsVo> collect = attrGroupEntities.stream().map((item) -> {
            AttrtGroupWithAttrsVo vos = new AttrtGroupWithAttrsVo();
            BeanUtils.copyProperties(item, vos);
            //2.查出每个属性分组的所有属性
            List<AttrEntity> attrs = attrService.getRelationAttr(vos.getAttrGroupId());
            vos.setAttrs(attrs);
            return vos;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //
        List<SpuItemAttrGroupVo> vos =  this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return  vos;
    }

}